package com.zhongzhi.nn.push.server;

import com.zhongzhi.nn.push.configure.ApplicationConstants;
import com.zhongzhi.nn.push.dao.NoticeInfoMapper;
import com.zhongzhi.nn.push.dataSource.MybatisConstants;
import com.zhongzhi.nn.push.model.LandInfo;
import com.zhongzhi.nn.push.model.NoticeInfo;

import com.zhongzhi.nn.push.util.PushNNUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.session.SqlSession;

import java.io.UnsupportedEncodingException;
import java.util.List;

public class PushNoticeInfo {

     //测试地址
     //private  String url="https://www.nnggzy.org.cn:3080/gxnnztbtest1/JSGCZtbMis2_JZ/Pages/Webservice/TdjyInfo.asmx";
      //正式地址
    private String url = ApplicationConstants.url;
    public static Log log = LogFactory.getLog(PushNoticeInfo.class);


    private String getPushResult(String xml, NoticeInfo notice) {
        log.info("推送数据："+xml);
        String webServiceXml = null;
        webServiceXml= PushNNUtils.getWebServiceXml(xml,"SetTDInfo");
        String resultxml = PushNNUtils.sendForWebService(url, webServiceXml);
        log.info("返回数据："+resultxml);
        String result = PushNNUtils.getXmlAttribute(resultxml);
        if("1".equals(result) || "项目名称已存在！".equals(result)){
            updateData(notice.getNoticeGuid());
        }
        return result;

    }

    /**
     * 招标公告
     * @return
     */
    public  String pushNotice() throws UnsupportedEncodingException {
        String xml = null;
        String message = "0"; //返回0代表没有土地数据
        //土地
        PushLandInfo land = new PushLandInfo();
        List<NoticeInfo> list = this.get();
        List<LandInfo> lands = null;
        for (NoticeInfo notice: list) {
            lands= land.get(notice.getProjectGuid());
            if(lands.size()>0) {
                xml = PushNNUtils.getNoticeResultXml(notice, lands);
                String result = getPushResult(xml, notice);
                if("1".equals(result)){
                    message="SUCCESS";
                    log.info("招标推送成功！");
                }else{
                    message=result; //返回错误信息
                    log.error("招标公告推送失败："+result);
                }
            }
        }
        return message;
    }

    /**
     * 变更公告
     * @return
     */
    public  String pushChangeNotice() throws UnsupportedEncodingException {
        String xml = null;
        String message = "0"; //没有土地数据
        //土地
        PushLandInfo land = new PushLandInfo();
        List<NoticeInfo> list = this.getChange();
        List<LandInfo> lands = null;
        for (NoticeInfo notice: list) {
            lands= land.get(notice.getProjectGuid());
            if(lands.size()>0) {
                xml = PushNNUtils.getChangeNoticeResultXml(notice, lands);
                String result = getPushResult(xml, notice);
                if ("1".equals(result)) {
                    message = "SUCCESS";
                    log.info("变更公告推送成功！");
                } else {
                    message = result;
                    log.error("变更公告推送失败："+result);
                }
            }
        }


        return message;
    }

    /**
     * 中标公告
     * @return
     */
    public  String  pushZBNotice() throws UnsupportedEncodingException {
        String xml = null;
        String message = "0"; //
        //土地
        PushLandInfo land = new PushLandInfo();
        List<NoticeInfo> list = this.getZBData();
        List<LandInfo> lands = null;
        for (NoticeInfo notice: list) {
            lands= land.get(notice.getProjectGuid());
            if(lands.size()>0){
                xml = PushNNUtils.getZBNoticeResultXml(notice, lands);
                String result = getPushResult(xml, notice);
                if("1".equals(result)){
                    updateZBData(notice.getNoticeGuid());
                }
                if("1".equals(result)){
                    message="SUCCESS";
                    log.info("中标公告推送成功！");
                }else{
                    message=result;
                    log.error("中标公告推送失败:"+result);
                }
            }

        }
        return message;
    }



    /**.
     * 查询新增公告数据
     *
     * @return
     */
    public List<NoticeInfo> get() {
        List<NoticeInfo> list = null;
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisConstants.getSqlSession();
            NoticeInfoMapper dao = sqlSession.getMapper(NoticeInfoMapper.class);
            list = dao.getData();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(sqlSession != null) {
                sqlSession.close();
            }
        }
        return list;
    }

    /**
     * 查询变更公告数据
     * @return
     */
    public List<NoticeInfo> getChange(){
        List<NoticeInfo> list = null;
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisConstants.getSqlSession();
            NoticeInfoMapper dao = sqlSession.getMapper(NoticeInfoMapper.class);
            list = dao.getChangeData();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(sqlSession != null) {
                sqlSession.close();
            }
        }
        return list;
    }

    /**
     * 查询中标数据
     * @return
     */
    public List<NoticeInfo> getZBData(){
        List<NoticeInfo> list = null;
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisConstants.getSqlSession();
            NoticeInfoMapper dao = sqlSession.getMapper(NoticeInfoMapper.class);
            list = dao.getCjData();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(sqlSession != null) {
                sqlSession.close();
            }
        }
        return list;
    }

    public Integer updateData(String noticeid) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisConstants.getSqlSession();
            NoticeInfoMapper dao = sqlSession.getMapper(NoticeInfoMapper.class);
            Integer count = dao.updateData(noticeid);
            sqlSession.commit();
            if(count>0){
                System.out.println("修改状态成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(sqlSession != null) {
                sqlSession.close();
            }
        }
        return 0;
    }

    public Integer updateZBData(String noticeid) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisConstants.getSqlSession();
            NoticeInfoMapper dao = sqlSession.getMapper(NoticeInfoMapper.class);
            Integer count = dao.updateZBData(noticeid);
            sqlSession.commit();
            if(count>0){
                System.out.println("修改中标资源状态成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(sqlSession != null) {
                sqlSession.close();
            }
        }
        return 0;
    }

}
